﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using QuanLyKho.DataAccess;
using QuanLyKho.Common;
using QuanLyKho.GUI.Library;
using QuanLyKho.GUI.Frm;
using System.Xml;

namespace QuanLyKho.GUI
{
    public partial class ucListMaterial : UserControl
    {
        private int _ID = 0;
        private long _ReID = 0;
        clsDataAccessListMaterial clsdtListMaterial = new clsDataAccessListMaterial();
        frmMaterial frm = null;
        private string strName = string.Empty;
        private DataSet dsTemp = new DataSet();
        private string NavClicked = "";
        private int totalRows = 25;
        public ucListMaterial()
        {
            InitializeComponent();
            strName = string.Empty;
            binDataListMaterial(strName);
        }
        private enum NavButton
        {
            First = 1,
            Next = 2,
            Previous = 3,
            Last = 4,
        }
        private void binDataListMaterial(string _Name)
        {
            List<clsListMaterial> ListMaterial = new clsDataAccessListMaterial().SelectAllWithName(_Name);
            DataTable dt = new DataTable("Material");
            if (ListMaterial.Count == 0)
            {
                dgvListMaterial.DataSource = dt;
                return;
            }
            else
            {
                dt.Columns.Add("Check", typeof(CheckBox));
                dt.Columns.Add("ID", typeof(long));
                dt.Columns.Add("STT", typeof(int));
                dt.Columns.Add("Mã vật tư", typeof(string));
                dt.Columns.Add("Tên vật tư", typeof(string));
                dt.Columns.Add("Ghi chú", typeof(string));
                dt.Columns.Add("Đơn vị", typeof(string));
                dt.Columns.Add("Chủng loại", typeof(string));
                dt.Columns.Add("Đặc tả", typeof(string));
                dt.Columns.Add("Nhà cung cấp", typeof(long));
                DataRow dr;
                int stt = 0;
                foreach (clsListMaterial obj in ListMaterial)
                {
                    dr = dt.NewRow();
                    stt = stt + 1;
                    dr[0] = (CheckBox)new CheckBox();
                    dr[1] = obj.ID;
                    dr[2] = stt;
                    dr[3] = obj.Code;
                    dr[4] = obj.Name;
                    dr[5] = obj.Des;
                    dr[6] = obj.Unit;
                    dr[7] = obj.Category;
                    dr[8] = obj.Specification;
                    dr[9] = obj.IDSupplier;
                    dt.Rows.Add(dr);
                }
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                dsTemp = ds;
                if (ds.Tables[0].Rows.Count > 0)
                    new clsPaging().fillDataGrid(ds, bindingSource1, dgvListMaterial, "", lblPaging, totalRows);
                else
                {
                    bindingSource1.DataSource = null;
                    bindingSource1.Clear();
                    dgvListMaterial.DataSource = bindingSource1;
                }
                if (dgvListMaterial.ColumnCount > 0)
                {
                    dgvListMaterial.Columns[1].Visible = false;
                    dgvListMaterial.Columns[2].Visible = false;//
                    dgvListMaterial.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//stt
                    dgvListMaterial.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;//Mavt
                    dgvListMaterial.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;//TenVT
                    dgvListMaterial.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//GHiChu
                    dgvListMaterial.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//Đơn vi
                    dgvListMaterial.Columns[8].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//Chungloai
                    dgvListMaterial.Columns[9].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//Đặc tả
                    dgvListMaterial.Columns[10].Visible = false;//Nha cung cap
                }
            }
        }
        private void ShowForm(long _ID)
        {
            if (_ID < 1)
                frm = new frmMaterial();
            else
                frm = new frmMaterial(_ID);
            frm.StartPosition = FormStartPosition.CenterParent;
            frm.ShowInTaskbar = false;
            frm.MaximizeBox = false;
            frm.MinimizeBox = false;
            frm.ShowDialog();
            binDataListMaterial(strName);
        }
        private void butDel_Click(object sender, EventArgs e)
        {
            int i = 0;


            if (Convert.ToInt32(dgvListMaterial.CurrentRow.Cells["status"].Value) == 0)
            {
                new clsLibrary().runLoading("Chọn nhà cung cấp muốn xóa!", 0);
                HavitelMessageBox.Show("Chọn nhà cung cấp muốn xóa!", "Thông báo", HavitelMessageBox.HavitelMessageButton.OK, HavitelMessageBox.HavitelMessageIcon.Warning);
            }
            if (Convert.ToInt32(dgvListMaterial.CurrentRow.Cells["status"].Value) == 1)
            {
                new clsLibrary().runLoading("Bạn đã chọn nhà cung cấp!", 0);
                if (HavitelMessageBox.Show("Bạn có muốn xóa tài khoản này?", "Thông báo", HavitelMessageBox.HavitelMessageButton.OKCancel, HavitelMessageBox.HavitelMessageIcon.Question) == DialogResult.OK)
                {
                    foreach (DataGridViewRow row in dgvListMaterial.Rows)
                    {

                        if (Convert.ToInt32(row.Cells["status"].Value) == 1)
                        {

                            long ID = Convert.ToInt64(row.Cells["ID"].Value.ToString());

                            {
                                new clsDataAccessListMaterial().Delete(ID);
                                i++;
                            }

                        }
                    }
                    new clsLibrary().runLoading("Bạn đã xóa nhà cung cấp!", 2);


                }
                else
                {
                    new clsLibrary().runLoading("Chọn nhà cung cấp muốn xóa!", 0);
                }
            }
            binDataListMaterial(strName);


        }
        private void ButEdit_Click(object sender, EventArgs e)
        {
            int i = 0;
            long ID;

            foreach (DataGridViewRow row in dgvListMaterial.Rows)
            {
                if ((Convert.ToInt32(row.Cells["status"].Value) == 1) && (Convert.ToInt64(dgvListMaterial.CurrentRow.Cells["status"].Value) == 1))
                    i++;

            }
            if (i > 1)
            {
                new clsLibrary().runLoading("Chọn vật tư muốn sửa!", 0);
            }
            if (i == 1)
            {
                ID = Convert.ToInt64(dgvListMaterial.CurrentRow.Cells["ID"].Value.ToString());
                ShowForm(ID);
                new clsLibrary().runLoading("Ðã sửa thành công!", 2);

            }
            if (i == 0)
            {
                new clsLibrary().runLoading("Chọn vật tư muốn sửa!", 0);

            }
        }
        private void chkCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCheckAll.Checked == true)
            {
                foreach (DataGridViewRow row in dgvListMaterial.Rows)
                {
                    ((DataGridViewCheckBoxCell)row.Cells["Status"]).Value = true;
                }
            }
            else
            {
                foreach (DataGridViewRow row in dgvListMaterial.Rows)
                {
                    ((DataGridViewCheckBoxCell)row.Cells["Status"]).Value = false;
                }
            }
        }
        private void butAdd_Click(object sender, EventArgs e)
        {
            ShowForm(0);
        }
        private void tsbFirst_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.First.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, dgvListMaterial, NavClicked, lblPaging, totalRows);

        }

        private void tsbPrevious_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.Previous.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, dgvListMaterial, NavClicked, lblPaging, totalRows);
        }

        private void tsbNext_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.Next.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, dgvListMaterial, NavClicked, lblPaging, totalRows);
        }
        private void tsbLast_Click(object sender, EventArgs e)
        {
            NavClicked = NavButton.Last.ToString();
            new clsPaging().fillDataGrid(dsTemp, bindingSource1, dgvListMaterial, NavClicked, lblPaging, totalRows);
        }
        private void butOutXml_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Text files (*.xml)|*.xml|All files (*.*)|*.*";
            saveFileDialog.Title = "Chọn file XML";
            List<clsListMaterial> ListMaterial = new clsDataAccessListMaterial().SelectAllWithName("");
            DataTable dt = new DataTable("listMaterial");
            dt.Columns.Add("ID", typeof(long));
            dt.Columns.Add("Code", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Des", typeof(string));
            dt.Columns.Add("Unit", typeof(string));
            dt.Columns.Add("Specification", typeof(string));
            dt.Columns.Add("Category", typeof(string));
            dt.Columns.Add("IDSupplier", typeof(long));
            DataRow dtrow;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                foreach (clsListMaterial obj in ListMaterial)
                {
                    dtrow = dt.NewRow();
                    dtrow[0] = obj.ID;
                    dtrow[1] = obj.Code;
                    dtrow[2] = obj.Name;
                    dtrow[3] = obj.Des;
                    dtrow[4] = obj.Unit;
                    dtrow[5] = obj.Category;
                    dtrow[6] = obj.Specification;
                    dtrow[7] = obj.IDSupplier;
                    dt.Rows.Add(dtrow);
                }
                DataSet ds = new DataSet("ListMaterial");
                ds.Tables.Add(dt);
                ds.WriteXml(saveFileDialog.FileName, XmlWriteMode.IgnoreSchema);
                new clsLibrary().runLoading("Đã lưu dữ liệu ra file : '" + saveFileDialog.FileName + "'.", 2);
            }
        }

        private void butInXML_Click(object sender, EventArgs e)
        {
            OpenFileDialog Open = new OpenFileDialog();
            DataTable dt = new DataTable();
            if (Open.ShowDialog() == DialogResult.OK)
            {
                XmlDataDocument xmlDatadoc = new XmlDataDocument();
                xmlDatadoc.DataSet.ReadXml("" + Open.FileName + "");
                DataSet dsXml = new DataSet("ListSupplier");
                dsXml = xmlDatadoc.DataSet;
                dt.Columns.Add("Check", typeof(CheckBox));
                dt.Columns.Add("ID", typeof(long));
                dt.Columns.Add("STT", typeof(int));
                dt.Columns.Add("Mã vật tư", typeof(string));
                dt.Columns.Add("Tên vật tư", typeof(string));
                dt.Columns.Add("Ghi chú", typeof(string));
                dt.Columns.Add("Đơn vị", typeof(string));
                dt.Columns.Add("Chủng loại", typeof(string));
                dt.Columns.Add("Đặc tả", typeof(string));
                dt.Columns.Add("Nhà cung cấp", typeof(long));
                DataRow dr;
                int stt = 0;
                foreach (DataRow Row in dsXml.Tables[0].Rows)
                {
                    dr = dt.NewRow();
                    stt = stt + 1;
                   
                    dr[0] = (CheckBox)new CheckBox();
                    dr[1] = Row["ID"].ToString();
                    dr[2] = stt;
                    dr[3] = Row["Code"].ToString();
                    dr[4] = Row["Name"].ToString();
                    dr[5] = Row["Des"].ToString();
                    dr[6] = Row["Unit"].ToString();
                    dr[7] = Row["Category"].ToString();
                    dr[8] = Row["Specification"].ToString();
                    dr[9] = Row["IDSupplier"].ToString();
                    dt.Rows.Add(dr);
                }
                DataSet ds = new DataSet();
                ds.Tables.Add(dt);
                dsTemp = ds;
                if (ds.Tables[0].Rows.Count > 0)
                    new clsPaging().fillDataGrid(ds, bindingSource1, dgvListMaterial, "", lblPaging, totalRows);
                else
                {
                    bindingSource1.DataSource = null;
                    bindingSource1.Clear();
                    dgvListMaterial.DataSource = bindingSource1;
                }
                if (dgvListMaterial.ColumnCount > 0)
                {
                    dgvListMaterial.Columns[1].Visible = false;
                    dgvListMaterial.Columns[2].Visible = false;//
                    dgvListMaterial.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//stt
                    dgvListMaterial.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;//Mavt
                    dgvListMaterial.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;//TenVT
                    dgvListMaterial.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//GHiChu
                    dgvListMaterial.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//Đơn vi
                    dgvListMaterial.Columns[8].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//Chungloai
                    dgvListMaterial.Columns[9].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;//Đặc tả
                    dgvListMaterial.Columns[10].Visible = false;//Nha cung cap
                }
            }
        }
        private void dgvListMaterial_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }


}



